#include <json/json.h>
#include <memory>
#include <iostream>
#include <sstream>
bool Serialize(const Json::Value &val, std::string &dest)
{
    // 先构造一个工厂类
    Json::StreamWriterBuilder swb;
    // 获取一个writer对象
    std::unique_ptr<Json::StreamWriter> writer(swb.newStreamWriter());
    std::stringstream ss;
    int ret = writer->write(val, &ss);
    if (ret != 0)
    {
        std::cout << "Json反序列化失败" << std::endl;
        return false;
    }
    dest = ss.str();
    return true;
}
bool Deserialize(Json::Value &val, const std::string &src)
{
    // 先构造一个工厂类
    Json::CharReaderBuilder crb;
    // 获取一个reader对象
    std::unique_ptr<Json::CharReader> reader(crb.newCharReader());
    std::string err;
    int ret = reader->parse(src.c_str(), src.c_str() + src.size(), &val, &err);
    if (ret == false)
    {
        std::cout << "Json序列化失败" << std::endl;
        return false;
    }
    return true;
}
int main()
{
    char name[] = "zhong";
    int age = 21;
    float score[3] = {99.1, 92.6, 12.5};
    Json::Value stu;

    stu["姓名"] = name;
    stu["年龄"] = age;
    stu["成绩"].append(score[0]);
    stu["成绩"].append(score[1]);
    stu["成绩"].append(score[2]);

    std::string stu_string;
    bool ret = Serialize(stu, stu_string);
    std::cout << stu_string << std::endl;

    Json::Value de_stu;
    ret = Deserialize(de_stu, stu_string);
    std::cout<<de_stu["姓名"].asString() <<std::endl; 
    std::cout<<de_stu["年龄"].asInt() <<std::endl; 
}